import { Socket } from '../Socket';
import * as WebSocket from 'ws';
import { Operater } from '../Operater';
import { LoginManager } from './LoginManager';
import com = require('com');

//数据库链接成功--启动manager
export class DBManager
{
    private static _instance:DBManager;
    private _socket:Socket;

    constructor()
    {
        this.init();
    }

    public static getInstance():DBManager
    {
        if(this._instance == null)
        {
            this._instance = new DBManager();
        }
        return this._instance;
    }

    private init():void
    {
        this._socket = new Socket();
        this._socket.init(3000);
    }

    /**解析socket请求 */
    public messageParse(ws:WebSocket,msg:any):void
    {
        console.log('DB receive:',msg);
        let obj:com.BaseProto = JSON.parse(msg);
        console.log('head_name:',obj.head_name);
        console.log('body_name:',obj.body_name);
        // Operater.query();
        switch(obj.head_name)
        {
            case com.HeadName.login:
                LoginManager.getInstance().messageParse(ws,obj);
                break;
        }
    }
}